package cn.com.xy.sms.sdk.db.entity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;
import cn.com.xy.sms.sdk.Iservice.OnlineUpdateCycleConfigInterface;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.DBManager;
import cn.com.xy.sms.sdk.db.XyCursor;
import cn.com.xy.sms.sdk.dex.DexUtil;
import cn.com.xy.sms.sdk.log.LogManager;

public class MovieMessageManager {

    private static final String TAG = "UrlMessageMovieManager";

    public static final String KEY_ID = "_id";
    public static final String KEY_NAME = "movie_name";
    public static final String KEY_DATA = "data";
    public static final String KEY_HASDATA = "hasdata";
    public static final String KEY_DATE = "date";

    public static final String TABLE_NAME = "tb_url_msg_movie";

    public static final String DROP_TABLE = " DROP TABLE IF EXISTS " + TABLE_NAME;
    public static final String CREATE_TABLE = "create table  if not exists " + TABLE_NAME + " (" + KEY_ID
            + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_HASDATA + " integer default 0," + KEY_DATE + " integer default 0," + KEY_DATA + " TEXT"
            + ")";

    private static final long URL_EFFECTIVE_TIME = 15 * 24 * 3600 * 1000; // 15
                                                                          // day\

    public static String getDefaultOrder() {
        return null;
    }

    public static long insert(ContentValues values) {
        try {
            if (values == null || values.size() <= 0) {
                return -1;
            }

            long rowId = DBManager.insert(TABLE_NAME, values);
            if (rowId < 0) {
                return rowId;
            }

            return rowId;
        } catch (Throwable ex) {
            LogManager.e(TAG, ex.getMessage());
        }
        return -1;
    }

    public static int delete(String where, String[] whereArgs) {
        try {
            int count = DBManager.delete(TABLE_NAME, where, whereArgs);
            return count;
        } catch (Throwable ex) {
            LogManager.e(TAG, ex.getMessage());
        }
        return -1;
    }

    public static int update(ContentValues values, String userWhere, String[] whereArgs) {
        try {
            if (values == null || values.size() <= 0) {
                return 0;
            }

            int count = DBManager.update(TABLE_NAME, values, userWhere, whereArgs);
            return count;
        } catch (Throwable ex) {
            LogManager.e(TAG, ex.getMessage());
        }
        return -1;
    }

    public static Cursor query(String[] projection, String selection, String[] selectionArgs, String sort) {
        try {
            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
            qb.setTables(TABLE_NAME);

            Cursor cursor = qb.query(DBManager.getSQLiteDatabase(), projection, selection, selectionArgs, null, null,
                    sort == null ? getDefaultOrder() : sort);
            return cursor;
        } catch (Throwable ex) {
            LogManager.e(TAG, ex.getMessage());
        }
        return null;

    }

    public static void insertOrUpdateMovie(String key, ContentValues values) {
        if (queryMovie(key) != null) {
            update(values, KEY_NAME + "=?", new String[] { key });
        } else {
            insert(values);
        }
    }

    public static MovielMessageItem queryMovie(String key) {
        MovielMessageItem item = null;
        Cursor cursor = query(null, KEY_NAME + "=?", new String[] { key }, null);
        if (cursor != null && cursor.moveToFirst()) {
            item = new MovielMessageItem(cursor);
        }

        if (cursor != null) {
            cursor.close();
        }

        if (item != null && System.currentTimeMillis() > (item.getDate() + URL_EFFECTIVE_TIME)) {
            delete(KEY_NAME + "=?", new String[] { key });
            item = null;
        }

        return item;
    }

    public static String queryCheckTimeExpireData(String movieName) {
        String moviewName = "";
        long cyc = DexUtil.getUpdateCycleByType(OnlineUpdateCycleConfigInterface.TYPE_URL_PREVIEW_CYCLE, Constant.DAY);

        long startTime = System.currentTimeMillis() - cyc;
        XyCursor cur = null;
        try {
            cur = DBManager.query(TABLE_NAME, new String[] { KEY_NAME },
                    KEY_NAME + " = ? and ( " + KEY_DATE + " < ? or " + KEY_HASDATA + " != ? ) ",
                    new String[] { movieName, startTime + "", "0" });

            if (cur != null && cur.getCount() > 0) {
                int movieNameIndex = cur.getColumnIndex(KEY_NAME);
                cur.moveToNext();
                moviewName = cur.getString(movieNameIndex);
            }
        } catch (Throwable e) {
            // e.printStackTrace();
            LogManager.e(Constant.TAG, "queryCheckTimeExpireData: " + e.getMessage(), e);

        } finally {
            XyCursor.closeCursor(cur, true);
        }
        return moviewName;
    }
}
